#ifndef __PCAP_MONITOR_H__
#define __PCAP_MONITOR_H__

#include <QObject>

#include <string>
#include <list>
#include <map>
#include <vector>
#include <atomic>

struct pcap;
namespace std {
class thread;
}
class PcapMonitor : public QObject
{
    Q_OBJECT
public:
    PcapMonitor(QObject *parent = nullptr);
    ~PcapMonitor();

    static std::vector<std::string> getAllNetInterfaceNames();
    static std::string makeMonitorRule(std::list<std::string> ips, std::list<std::string> ports);

    void setMonitorRule(const std::string &rule);
    bool start(const std::string &interface = "eno1");
    void stop();

Q_SIGNALS:
    void accessNotify(const QString &ip, int port);

private:
    void notify(const std::string &ip, const uint16_t port);
    friend void packet_handler(u_char *, const struct pcap_pkthdr *, const u_char *);
    friend void run(struct pcap *, void *);

private:
    struct pcap *m_pcapHandle;
    std::thread *m_thread;
    std::string m_rule;
    std::atomic<bool> m_isRunning;

    using RecordKey = std::pair<std::string, uint16_t>;
    std::map<RecordKey, long> m_records;
};

#endif